var req;
var routes;
var selected;
var flightNo;
var fDate;
var luggage;
var loc;
var flight;
var checkedIn;
var tmpArrLoc;
var manual;
var errorMsg;
var at_airport_segments;
var error;

function getRoutes() {


   var form = document.getElementById("mainFrm");
   errorMsg="";
   flightNo = form.elements[0].value;
   flight = flightNo.substring(0,2);
   flight = flight.toUpperCase();

   flightNo = flight + flightNo.substring(2);
   fDate = form.elements[1].value;
   loc =  form.elements[2].value;
   tmpLoc = loc.substring(0,1);
   tmpLoc = tmpLoc.toUpperCase();
   loc = tmpLoc + loc.substring(1);
   
   luggage = form.elements[3].checked;
   checkedIn = form.elements[4].checked;
   var url = "/webgui/Neo4jServlet?flightNo="+encodeURIComponent(flightNo)+"&fDate="+encodeURIComponent(fDate)+"&location="+encodeURIComponent(loc)+"&luggage="+luggage+"&checkedIn="+checkedIn;
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url, true);
   req.onreadystatechange = callback;
   req.send(null);
   clearTable();
   var table = document.getElementById("appTable");
   var row = table.insertRow(table.rows.length);
   var cell = row.insertCell(0);
   table.rows[0].cells[0].innerHTML=errorMsg;
   cell.innerHTML = "Please wait while generating routes...";
   row = table.insertRow(table.rows.length);
   cell = row.insertCell(0);
   cell.innerHTML = "Flight data provided by <a href=\"http://www.avinor.no/\">Avinor</a>";
}



function init(){
    req;
    routes;
    selected;
    flightNo="";
    fDate=0;
    luggage=false;
    loc="Blindern";
    errorMsg="";
    checkedIn=false;
    tmpArrLoc;
    manual;
}

function generateMain() {
    clearTable();
    var table = document.getElementById("appTable");
    var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    table.rows[0].cells[0].innerHTML=errorMsg;
    var row;
    var cell;
    var date = new Date();
    date.setDate(date.getDate() + 2);
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="Flight Number: <input type=\"text\" name=\"flightNo\" value=\""+flightNo+"\" />";
    
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    if(fDate == 2){
        cell.innerHTML="Departing: <select name=\"flightDate\"><option value=\"0\">Today<option value=\"1\">Tomorrow<option value=\"2\" selected>"+days[date.getDay()]+"</select>";
    } else if(fDate == 1) {
        cell.innerHTML="Departing: <select name=\"flightDate\"><option value=\"0\">Today<option value=\"1\" selected>Tomorrow<option value=\"2\">"+days[date.getDay()]+"</select>";
    } else {
        cell.innerHTML="Departing: <select name=\"flightDate\"><option value=\"0\" selected>Today<option value=\"1\">Tomorrow<option value=\"2\">"+days[date.getDay()]+"</select>";
    }
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="Start your travel at: <input type=\"text\" name=\"location\" value=\""+loc+"\" />";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    
    if(luggage==true){
        cell.innerHTML="Do you need to check in luggage? <input type=\"checkbox\" name=\"luggage\" value=\"lugg\"  checked />";
    } else {
        cell.innerHTML="Do you need to check in luggage? <input type=\"checkbox\" name=\"luggage\" value=\"lugg\" />";
    }
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    
    if(checkedIn==true){
        cell.innerHTML="Have you checked in? <input type=\"checkbox\" name=\"checkedIn\"  value=\"check\"  checked />";
    } else {
        cell.innerHTML="Have you checked in? <input type=\"checkbox\" name=\"checkedIn\"  value=\"check\" />";
    }
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<input type=\"button\" onclick=\"getRoutes()\" value=\"GoToGate\" />";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "Flight data provided by <a href=\"http://www.avinor.no/\">Avinor</a>";
    var form = document.getElementById("mainFrm");
    form.elements[0].focus();
}

function generateAvailable() {
    clearTable();
    var table = document.getElementById("appTable");
    errorMsg=""
    
    var row;
    var cell;
    var id;
    if(routes.length>0){
        row = table.insertRow(table.rows.length);
        cell = row.insertCell(0);
        cell.innerHTML="<h2>Available Routes From "+routes[0].segments[0].from+" to "+routes[0].segments[routes[0].segments.length-1].to+": </h2>";
        for(var i=0;i<routes.length;i++){
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            if(routes[i].time!=0){
            	cell.innerHTML="<table onclick=\"clickedT("+i+");\" onmouseover=\"this.style.backgroundColor='lightgray';\" onmouseout=\"this.style.backgroundColor='transparent';\"><tr><td class=\"standard_cell\"\">Departure time: "+routes[i].segments[0].dep.substring(0,2)+":"+routes[i].segments[0].dep.substring(2)+"</td><td class=\"right_cell\">Arrival time: "+routes[i].segments[routes[i].segments.length-1].arr.substring(0,2)+":"+routes[i].segments[routes[i].segments.length-1].arr.substring(2)+"</td></tr><tr><td class=\"standard_cell\">Number of transfers: "+routes[i].transfers+"</td><td class=\"right_cell\">Total transportation time: "+routes[i].time+"</td></tr></table><hr />";
            } else {
            	cell.innerHTML="<table onclick=\"clickedT("+i+");\" onmouseover=\"this.style.backgroundColor='lightgray';\" onmouseout=\"this.style.backgroundColor='transparent';\"><tr><td class=\"standard_cell\"\">Departure time: "+routes[i].segments[0].dep.substring(0,2)+":"+routes[i].segments[0].dep.substring(2)+"</td><td class=\"right_cell\">Arrival time: "+routes[i].segments[routes[i].segments.length-1].arr.substring(0,2)+":"+routes[i].segments[routes[i].segments.length-1].arr.substring(2)+"</td></tr><tr><td class=\"standard_cell\">Number of transfers: "+routes[i].transfers+"</td><td class=\"right_cell\"></td></tr></table><hr />";
            }
        }
    } else {
    	 var dep = at_airport_segments[0].dep;
         errorMsg="<b class=\"errorMessage\">There are no available routes from \""+loc+"\" arriving at the airport before "+dep.substring(0,2)+":"+dep.substring(2)+". Please create a route manually, or go back to main to enter another location.</b>";
    }
    table.rows[0].cells[0].innerHTML=errorMsg;
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<input type=\"button\" onclick=\"backToMain()\" value=\"Back to main\" /><input type=\"button\" onclick=\"generateManual()\" value=\"Manual planner\" />";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "Flight data provided by <a href=\"http://www.avinor.no/\">Avinor</a>";
}

function generateShowRoute() {
    clearTable();
    var index = selected;
    errorMsg = ""
    var mainTcell = document.getElementById("appTable").rows[0].cells[0];
	mainTcell.innerHTML=errorMsg;
    
    var table = document.getElementById("appTable");
    var row;
    var cell;
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    if(flight.destCode=="undefined"){
        flight.destCode = "";
    }
    cell.innerHTML="<h2>Selected Route for Flight "+flightNo+": "+ flight.destCode+"</h2>"; 
    
    var i;
    for(i=0;i<routes[index].segments.length;i++){
        if (routes[index].segments[i].type=="at_airport_info"){
            break;
        } else {
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            cell.innerHTML="<table><tr><td class=\"type_cell\">"+routes[index].segments[i].type+"</td></tr><tr><td class=\"standard_cell\">Departs: "+routes[index].segments[i].from+"</td><td class=\"time_cell\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td></tr><tr><td class=\"standard_cell\">Arrives: "+routes[index].segments[i].to+"</td><td class=\"time_cell\">"+routes[index].segments[i].arr.substring(0,2)+":"+routes[index].segments[i].arr.substring(2)+"</td></tr><hr /></table>";
        } 
    }
    generateAtAirportInfo(index,i);
    if(checkedIn==true){
        if(luggage==false){
            if(flight.rushSecurity=="true"){
                row = table.insertRow(table.rows.length);
                cell = row.insertCell(0);
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Already checked in</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">No luggage</td></tr><hr /></table>";
            } else {
                row = table.insertRow(table.rows.length);
                cell = row.insertCell(0);
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Already checked in</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">No luggage</td></tr><hr /></table>";
            }
        } else {
            if(flight.rushBagDrop=="true"){
                row = table.insertRow(table.rows.length);
                cell = row.insertCell(0);
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Already checked in</td></tr><hr /></table>";
            } else {
                row = table.insertRow(table.rows.length);
                cell = row.insertCell(0);
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Already checked in</td></tr><hr /></table>";
            }
            i += 1
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            if(flight.rushSecurity=="true"){
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><hr /></table>";
            } else {
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><hr /></table>";
            }
        }
    } else {
        if(luggage==false){
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            if(flight.rushCheckIn=="true"){
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">No luggage</td></tr><hr /></table>";
            } else {
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">No luggage</td></tr><hr /></table>";
            }
            i+=1
        } else {
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            if(flight.rushCheckIn=="true"){
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"> </td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><hr /></table>";
            } else {
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><hr /></table>";
            }
            i+=1
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            if(flight.rushBagDrop=="true"){
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"> </td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><hr /></table>";
            } else {
                cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><hr /></table>";
            }
            i+=1
        }
        if(flight.rushSecurity=="true"){
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><hr /></table>";
        } else {
            row = table.insertRow(table.rows.length);
            cell = row.insertCell(0);
            cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><hr /></table>";
        }
            
    }
    i += 1
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    if(flight.rushGate=="true"){
        cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><tr><td class=\"time_cell\"></td><td class=\"standard_cell\">Arriving during rush hour, extra time added</td></tr><hr /></table>";
    } else {
        cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+routes[index].segments[i].dep.substring(0,2)+":"+routes[index].segments[i].dep.substring(2)+"</td><td class=\"standard_cell_bold\">Go to "+routes[index].segments[i].to+"</td></tr><hr /></table>";
    }
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+flight.gateClosed.substring(0,2)+":"+flight.gateClosed.substring(2)+"</td><td class=\"standard_cell_bold\">Gate Closed</td></tr><hr /></table>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<table><tr><td class=\"time_cell_bold\">"+flight.depTime.substring(0,2)+":"+flight.depTime.substring(2)+"</td><td class=\"standard_cell_bold\">Departure "+flight.flightNo+"</td></tr><hr /></table>";
    
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<input type=\"button\" onclick=\"backToMain()\" value=\"Back to main\" /> <input type=\"button\" onclick=\"generateAvailable()\" value=\"Show All Routes\" />";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<input type=\"button\" onclick=\"generateManual()\" value=\"Manual planner\" />";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "Flight data provided by <a href=\"http://www.avinor.no/\">Avinor</a>";
}
function generateManual(){
    
    clearTable();
    var table = document.getElementById("appTable");
    table.rows[0].cells[0].innerHTML=errorMsg;
    var row;
    var cell;
    tmpArrLoc="Airport";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<h2>Manual route planner</h2>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<table class=\"manual\" id=\"manualTable\"><tr><th>Going From</th><th>At Time</th><th>Going To</th><th>At Time</th><th>By Means Of</th><th> </th></tr></table><hr />";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<table id=\"inpTable\"><tr><td>Going From:</td><td><input type=\"text\" name=\"from\" value=\"Name\" onclick=\"value=\'\'\"/></td></tr><tr><td>Departure time: </td><td><input type=\"text\" name=\"depTime\" value=\"HHMM\" onclick=\"value=\'\'\"/></td></tr><tr><td>Arrival at "+tmpArrLoc+": </td><td><input type=\"text\" name=\"arrTime\" value=\"HHMM\" onclick=\"value=\'\'\"/></td></tr><tr><td>Transportation type: </td><td><input type=\"text\" name=\"type\" value=\"Type\" onclick=\"value=\'\'\"/></td></tr></table>";
   
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML="<input type=\"button\" value=\"Add segment\" onclick=\"addSegment()\"/> <input type=\"button\" value=\"Finished\" onclick=\"finishManual()\" /><input type=\"button\" value=\"Cancel\" onclick=\"cancelManual()\"/>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "Flight data provided by <a href=\"http://www.avinor.no/\">Avinor</a>";
    
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "Public transportation information suppliers:";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "<a href=\"http://trafikanten.no/\">Trafikanten</a>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "<a href=\"http://www.flytoget.no/\">Flytoget</a>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "<a href=\"http://flybussen.no/\">Flybussen</a>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "<a href=\"http://www.nsb.no/\">NSB</a>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "<a href=\"http://www.norgestaxi.no/\">Norgestaxi Oslo AS - 08000</a>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "<a href=\"http://www.oslotaxi.no/\">Oslo Taxi BA - 02323 </a>";
    row = table.insertRow(table.rows.length);
    cell = row.insertCell(0);
    cell.innerHTML = "<a href=\"http://www.citytaxi.no/\">City Taxi - 02322 </a>";
    
    var mainTcell = document.getElementById("appTable").rows[0].cells[0];
    reinitForm();
    if(manual==true){
        table=document.getElementById('manualTable');
        var counter = 1;
        segments = routes[selected].segments;
        for(var i=0;i<segments.length;i++){
            table.insertRow(counter++);
            cell = row.insertCell(0);
            cell.innerHTML=segments[i].from;
            cell = row.insertCell(1);
            cell.innerHTML=segments[i].dep;
            cell = row.insertCell(2);
            cell.innerHTML=segments[i].to;
            cell = row.insertCell(3);
            cell.innerHTML=segments[i].arr;
            cell = row.insertCell(4);
            cell.innerHTML=segments[i].type;
            cell = row.insertCell(5);
            cell.innerHTML="<input type=\"button\" onclick=\"delRow(this)\" value=\"X\" />";
        }
    }
}

function cancelManual(){
    generateAvailable();
}


function backToMain(){
    errorMsg="";
    generateMain();
}

function generateAtAirportInfo(index,i){
    at_airport_segments = [];
    var tab = document.getElementById("appTable");
    tab.rows[0].cells[0].innerHTML=errorMsg;
    for(var j=i;j<routes[index].segments.length;j++){
        var seg = new Object;
        seg.type = routes[index].segments[j].type;
        seg.to = routes[index].segments[j].to;
        seg.from = routes[index].segments[j].from;
        seg.dep = routes[index].segments[j].dep;
        seg.arr = routes[index].segments[j].arr;
        at_airport_segments.push(seg);
    }
}

function addSegment(){
    
    var table=document.getElementById('manualTable');
    var mainTcell = document.getElementById("appTable").rows[0].cells[0];
    errorMsg="";
	mainTcell.innerHTML=errorMsg;
    //gives the name of the destination...
    var dest = tmpArrLoc;
    var form=document.getElementById("mainFrm");
    var num = form.elements.length-3;
    var rows=table.rows;
    var index=1;
    var i;
    //find right location
    for(i=1;i<rows.length;i++){
        if(dest==rows[i].cells[0].innerHTML){
            index=i+1;
            break;
        }
    }

    
    var row = table.insertRow(index);
    var cell;
    var counter = 0;
    for (i=num-4;i<num;i++){
      if(counter==2){
        cell = row.insertCell(counter++);
        cell.innerHTML=dest;
      }
      if(counter>1){
        cell = row.insertCell(counter++);
      } else {  
        cell = row.insertCell(counter++);	
      }
      cell.innerHTML=form.elements[i].value;
      
    }
    tmpArrLoc = form.elements[num-4].value; //value of 'from' text field
    
    cell = row.insertCell(counter);
    cell.innerHTML="<input class=\"deleteButton\" type=\"button\" onclick=\"delRow(this)\" value=\"X\" />";
    table = document.getElementById("inpTable");
    //get row with field for arrival time
	var newArr=table.rows[2].cells[0];
	newArr.innerHTML="Arrival time at "+tmpArrLoc+": ";
    
	reInitForm();
	checkRouteValidity();
}

function finishManual() {
	var table=document.getElementById('manualTable');
	manual = true;
	route = new Object;
    route.time = 0;
    route.transfers = 0;
    var prevTime="0";
    route.segments = [];
    if(table.rows.length-1>0){
	    for(var i = table.rows.length-1; i >= 1;i--){
	        segment = new Object;
	        segment.from = table.rows[i].cells[0].innerHTML;
	        segment.dep = table.rows[i].cells[1].innerHTML;
	        segment.to = table.rows[i].cells[2].innerHTML;
	        segment.arr = table.rows[i].cells[3].innerHTML;
	        segment.type = table.rows[i].cells[4].innerHTML;
	        if(prevTime!="0"){ 
	            route.transfers += 1;
	        }
	        prevTime = segment.dep;
	        route.segments.push(segment);
	    }
    }
    for(var i=0;i<at_airport_segments.length;i++){
        route.segments.push(at_airport_segments[i]);
    }
    
    selected = routes.length;
    routes.push(route);
    generateShowRoute();
}
function reInitForm(){
	var form = document.getElementById("mainFrm");
    var num = form.elements.length-4;
    form.elements[num--].value="Type";
    form.elements[num--].value="HHMM";
    form.elements[num--].value="HHMM";
	form.elements[num--].value="Name";
}

function delRow(r){
	var i=r.parentNode.parentNode.rowIndex;
	document.getElementById('manualTable').deleteRow(i);
	checkRouteValidity();
}
function checkRouteValidity(){
	var rows=document.getElementById('manualTable').rows;
	var previous = "";
    //get 'status' row
	var mainTcell = document.getElementById("appTable").rows[0].cells[0];
	var i;
    var err = false;
	for(i=1;i<rows.length;i++){
		if(previous=="" && rows[i].cells[2].innerHTML!="Airport"){
			mainTcell.innerHTML="<b class=\"errorMessage\">There is a missing link between "+rows[i].cells[0].innerHTML+" and the Airport!</b>";
            err = true;
			break;
		} else if(previous!="" && previous!=rows[i].cells[2].innerHTML){
            err = true;
			mainTcell.innerHTML="<b class=\"errorMessage\">There is a missing link between "+previous+" and "+rows[i].cells[2].innerHTML+"!</b>";
			break;
		}
		previous = rows[i].cells[0].innerHTML;
	}
	if(previous==""){
		previous = "Airport";
	}
    tmpArrLoc = previous;
	var arrAt=document.getElementById("inpTable").rows[2].cells[0];
    if(err==false){
        arrAt.innerHTML="Arrival time at "+previous+": ";
    } else {
        arrAt.innerHTML="Arrival time at <b class=\"errorMessage\">"+previous+"</b>: ";
    }
    
}
function clickedT(index){
    var table = document.getElementById("appTable");
    selected = index;
    generateShowRoute();
}
function clearTable(){
    var table = document.getElementById("appTable");
   var i;
   for (i=table.rows.length-1;i>0;i--){
        table.deleteRow(i);
   }
   
}

function callback() {
    if (req.readyState == 4) {
        if (req.status == 200) {    //ready and successfull
            processResponse();
            var tab = document.getElementById("appTable");
            clearTable();
            errorMsg = "An error was encountered while processing your request. Please click <a href=\"javascript:void(0)\" onclick=\"backToMain()\">here</a> to try again!"
            tab.rows[0].cells[0].innerHTML=errorMsg;
            if (error != undefined){
                if(error == "no flight"){
                    errorMsg="<b class=\"errorMessage\">Invalid flight number!</b>";
                    generateMain();
                } else if (error == "no routes"){
                	var tab = document.getElementById("appTable");
                	var dep = at_airport_segments[0].dep;
                    errorMsg="<b class=\"errorMessage\">Unable to find a route from \""+loc+"\" arriving at the airport before "+dep.substring(0,2)+":"+dep.substring(2)+". Please enter the information manually or call a taxi.</b>";
                    tab.rows[0].cells[0].innerHTML=errorMsg;
                    generateManual();
                } 
            } else {
                selected = 0;
                generateShowRoute();
            } 
            
        } else {
            var table = document.getElementById("appTable");
            clearTable();
            errorMsg="An error was encountered while processing your request. Please click <a href=\"javascript:void(0)\" onclick=\"backToMain()\">here</a> to try again!"
            table.rows[0].cells[0].innerHTML=errorMsg;
        }
    }
}

function goTo(url) {
    location.href = url;
}
function processResponse() {
    var form = document.getElementById("mainFrm");
    var myObject = eval('(' + req.responseText + ')');
    if(myObject.flight!=undefined){
        if(myObject.error != undefined){
            at_airport_segments = myObject.routes[0].segments;
            routes = [];
        } else {
            routes = myObject.routes;
        }
        flight = myObject.flight;
        error = myObject.error;
    } else{
        error = myObject.error;
    }
}
